Skip to content

[WEEK08-2] 이지현#37

Merged
sik9252 merged 2 commits into
mainfrom
sik9252
Apr 4, 2026
Merged

[WEEK08-2] 이지현#37
sik9252 merged 2 commits into
mainfrom
sik9252

Conversation

@sik9252
Copy link
Copy Markdown
Collaborator

@sik9252 sik9252 commented Apr 2, 2026

이렇게 풀었어요

1. Palindrome Linked List

  • 문제를 풀었어요.
  • 풀이 시간 : 30분

1) 복잡도 계산

시간 복잡도: O(n)

공간 복잡도: O(n)


2) 접근 아이디어

이 문제는 연결 리스트가 palindrome인지 판별하는 문제이다.
연결 리스트가 아직도 익숙하지 않기 때문에 그냥 값을 꺼내서 배열로 바꾸고 확인했다.
왼쪽 값과 오른쪽 값이 하나라도 다르면 바로 false를 반환하고, 끝까지 모두 같으면 true를 반환하도록 구현했다.


3) 회고

정석 풀이(?)는 리스트를 뒤집는 방식이라고 한다.


2. Move Zeroes

  • 문제를 풀었어요.
  • 풀이 시간 : 20분

1) 복잡도 계산

시간 복잡도: O(n)

공간 복잡도: O(1)


2) 접근 아이디어

이 문제는 배열의 모든 0을 뒤로 보내면서, 0이 아닌 숫자들의 상대적인 순서는 유지해야 하는 문제이다. 0이 아닌 값들만 먼저 앞으로 모으고, 나머지 칸을 0으로 채우면 되겠다고 생각했다.

그래서 배열을 한 번 순회하면서 0이 아닌 값만 앞에서부터 차례대로 덮어썼다. 이때 idx를 두어 다음 non-zero 값을 넣을 위치를 관리했다. 순회가 끝난 뒤에는 idx 이후 남은 위치들을 전부 0으로 채우도록 구현했다. 이렇게 하면 0이 아닌 값들의 순서는 유지하면서, 0은 자연스럽게 뒤쪽으로 이동하게 된다.


3) 회고

이 문제는 swap으로 풀 수도 있을 것 같다.

var moveZeroes = function (nums) {
  let insertPos = 0;

  for (let i = 0; i < nums.length; i++) {
    if (nums[i] !== 0) {
      [nums[insertPos], nums[i]] = [nums[i], nums[insertPos]];
      insertPos++;
    }
  }
};

Copy link
Copy Markdown
Collaborator

@raejun92 raejun92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

회고의 swap은 정말 생각도 못했네요! 고생하셨습니다

Comment thread sik9252/MoveZeroes.js
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오우 정석 그 자체네요!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 배열로 풀었는데ㅠ 투 포인터 방식을 익히면 좋을 것 같네요!

Copy link
Copy Markdown
Member

@doitchuu doitchuu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다 👍

}

return true;
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 left, right라는 변수명이 되게 직관적인 것 같아요.
투포인터를 활용해서도 많이 풀던데 이 방식대로 풀어도 좋을 것 같아요!

@sik9252 sik9252 merged commit 8f1c002 into main Apr 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants